﻿/* 
 * GeneralControl.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file acts as a controller to General tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.ServiceProcess;
using System.Diagnostics;

partial class GeneralControl : UserControl
{
    // Checks the status of OpenAfs service and apply changes based on the user input
    public override void Refresh()
    {
        clientServiceController.Refresh();
        switch (clientServiceController.Status)
        {
            case ServiceControllerStatus.Stopped:
                startServiceButton.Enabled = true;
                stopServiceButton.Enabled = false;
                stateTextBox.Text = "Stopped";
                serviceTimer.Enabled = false;
                break;
            case ServiceControllerStatus.Running:
                startServiceButton.Enabled = false;
                stopServiceButton.Enabled = true;
                stateTextBox.Text = "Running";
                serviceTimer.Enabled = false;
                break;
            case ServiceControllerStatus.StopPending:
                startServiceButton.Enabled = false;
                stopServiceButton.Enabled = false;
                stateTextBox.Text = "Stopping";
                serviceTimer.Enabled = true;
                break;
            case ServiceControllerStatus.StartPending:
                startServiceButton.Enabled = false;
                stopServiceButton.Enabled = false;
                stateTextBox.Text = "Starting";
                serviceTimer.Enabled = true;
                break;
            default:
                try{
                }
                catch (Exception e){
                    throw e;
                }
                break;
        }
    }

    // Starts the service
    private void startServiceButton_Click(object sender, EventArgs e)
    {
        clientServiceController.Start();
        Refresh();
    }

    // Stops the service
    private void stopServiceButton_Click(object sender, EventArgs e)
    {
        clientServiceController.Stop();
        Refresh();
    }

    private void applyButton_Click(object sender, EventArgs e)
    {

    }

    private void cancelButton_Click(object sender, EventArgs e)
    {
        Process[] p = Process.GetProcessesByName("mmc");
        p[0].Kill();
    }

    // Refreshes on a timer tick
    private void serviceTimer_Tick(object sender, EventArgs e)
    {
        Refresh();
    }
}